package ch.usi.inf.pf2.editor;

import ch.usi.inf.pf2.Sheet;


/**
 * MultiplePaste is useful to paste more cells.
 * 
 * @author Lorenzo Baracchi <lorenzo.baracchi@usi.ch>
 */
public class MultiplePaste extends MultipleTextManagement
{
    Paste[][] toPaste;
    
    
    /**
     * Create a Paste in more cells.
     * @param s The sheet where the multiple pasting is performed.
     * @param rowIndexes The rows of the selected cells.
     * @param columnIndexes The columns of the selected cells.
     * @param newCells The cells to be pasted.
     */
    public MultiplePaste (Sheet s, int[] rowIndexes, int[] columnIndexes, MultipleTextManagement newCells)
    {
    
	   toPaste=new Paste[newCells.getActions().length][newCells.getActions()[0].length];
	   for(int i=0; i<toPaste.length; i++)
			  for(int j=0; j<toPaste[i].length; j++)
			 try {
				toPaste[i][j]=new Paste(s, rowIndexes[0]+i+1, columnIndexes[0]+j+1, newCells.getActions()[i][j]);
			 } catch (Exception e)
			 {
				System.out.println("I can't paste!");
				assert false;
			 }	
    }
    
    @Override
    public void evaluate()
    {
    	try{
	   for(int i=0; i<toPaste.length; i++)
		  for(int j=0; j<toPaste[i].length; j++)
			 toPaste[i][j].evaluate();
    	}
    	catch (NullPointerException e)
    	{
    		// it's fine, ignore
    	}
    	catch (ArrayIndexOutOfBoundsException e)
    	{
    		// paste what is possible and the rest no matter
    	}
    }

    @Override
    public void restore()
    {
	   for(int i=0; i<toPaste.length; i++)
		  for(int j=0; j<toPaste[i].length; j++)
			 toPaste[i][j].restore();
    }

    @Override
    public TextManagement[][] getActions()
    {
	   return toPaste;
    }

}
